#include "stdafx.h"
#include "Texture.h"

#include "GlobalAppData.h"

using namespace mgfx;
using namespace mcore;

Texture::ResourceMap Texture::mResources = Texture::ResourceMap();

Texture * Texture::createFromFile(const std::string & fileName)
{
	ResourceMap::const_iterator found = mResources.find(fileName);
	if(found != mResources.end())
	{
		return found->second;
	}
	else
	{
		ID3D11ShaderResourceView * rv = 0;
		HR(D3DX11CreateShaderResourceViewFromFile(GlobalAppData::Instance.Device, fileName.c_str(), 0, 0, &rv, 0));

		Texture * tex = new Texture;
		tex->mResourceView = rv;
		mResources[fileName] = tex;

		return tex;
	}
}

void Texture::freeAll()
{
	ResourceMap::iterator itr, end = mResources.end();
	for(itr = mResources.begin() ; itr != end ; ++itr)
	{
		ReleaseCOM(itr->second->mResourceView);
		delete itr->second;
	}
}

Texture::Texture()
{

}

Texture::~Texture()
{

}